package com.imooc.Controller;


import com.imooc.bean.Friend;
import com.imooc.service.AuthService;
import com.imooc.task.IndexTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author lhy
 * @Date 2021/9/28 11:33
 */
@RestController
@Slf4j
public class IndexController {
    @Value("${server.port}")
    private String port;

    @Autowired
    private IndexTask indexTask;

    @GetMapping("test")
    public String test(){
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->{
            indexTask.dosame();
        });
        executorService.shutdown();
        return "ok";
    }


    @GetMapping("sayHi")
    public String sayHi(
            @RequestParam("timeOut")
            Integer timeOut
    ) throws InterruptedException {
        if (timeOut>0){
            while (timeOut-- >=0){
                Thread.sleep(1000);
            }

        }
        log.info("retry"+port);

        return "this is "+port;
    }



    @PostMapping("sayHi")
    public Friend sayHiPost(
            @RequestBody
            Friend friend){
        log.info("you are "+friend.getName());
        return friend;
    }

    @GetMapping("error")
    public String error(){
        throw new RuntimeException("block sheep;");
//        return "error";
    }


    @GetMapping("retry")
    public String retry(
            @RequestParam("time")
            Integer time
    ){
        try {
            log.info(""+time);
            Thread.sleep(time*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return port;
    }

    @GetMapping("getCustomDict")
    public String getCustomDict(HttpServletResponse response){

        response.setHeader("Last-Modified",System.currentTimeMillis()+"");
        response.setHeader("ETag","123");

        return  "ok";
    }

    @Autowired
    private AuthService authService;

    @GetMapping("getuser")
    public String getuser(){

        authService.logUser(null);

        return "ok";

    }



}
